<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('./import')">excel导入</el-button>
          <el-button type="danger" size="small" @click="doExport">excel导出</el-button>
          <el-button type="primary" size="small" @click="doAdd">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="照片">
            <template v-slot="scope">
              <img :src="scope.row.staffPhoto" width="100%">
            </template>
          </el-table-column>
          <el-table-column label="聘用形式">
            <template v-slot="scope">
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template v-slot="scope">
              <el-button type="text" size="small" @click="$router.push('/detail?id='+scope.row.id)">查看</el-button>
              <el-button type="text" size="small" @click="doEmployees(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="doDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="sizes , prev, pager, next"
            :current-page="page"
            :page-sizes="[2,5,10,20]"
            :page-size="size"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 弹框 -->
    <el-dialog :visible.sync="showDialog" title="新增员工">
      <empDialog v-if="showDialog" @success="hSuccess" @close="hClose" />
    </el-dialog>
    <!-- 分配角色弹框 -->
    <el-dialog :visible.sync="showDialogEmp" title="分配角色">
      <assignRole v-if="showDialogEmp" :cur-id="curId" @hSuccess="hSuccessRoels" />
    </el-dialog>
  </div>
</template>
<script>
import { getEmpList, delEmp } from '@/api/employess'
import employeeEnmu from '@/constant/employees'
import empDialog from './empDialog.vue'
import assignRole from './assignRole.vue'
const hireType = employeeEnmu.hireType
console.log(hireType)
export default {
  components: { empDialog, assignRole },
  data() {
    return {
      list: [],
      page: 1,
      size: 5,
      total: 0,
      showDialog: false,
      showDialogEmp: false,
      curId: ''
    }
  },
  created() {
    // 获取角色列表
    this.loadEmpList()
  },
  methods: {
    // 获取角色列表
    async loadEmpList() {
      const res = await getEmpList({ page: this.page, size: this.size })
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 聘用形式转化
    formatEmployment(num) {
      const item = hireType.find(it => it.id === num)
      return item ? item.value : '未知'
    },
    // 当页码发生变化时
    handleCurrentChange(page) {
      this.page = page
      this.loadEmpList()
    },
    // 当一页的数量发生变化时
    handleSizeChange(pagesize) {
      this.size = pagesize
      this.loadEmpList()
    },
    // 删除
    async doDel(id) {
      if (this.list.length === 1) {
        this.page--
        this.loadEmpList()
      }
      const res = await delEmp(id)
      console.log(res)
      this.loadEmpList()
      this.$message.success('删除成功')
    },
    // 新增员工
    doAdd() {
      this.showDialog = true
    },
    hSuccess() {
      this.showDialog = false
      this.total++
      this.page = Math.ceil(this.total / this.size)
      this.loadEmpList()
    },
    hClose() {
      this.showDialog = false
    },
    doExport() {
      import('@/vendor/Export2Excel').then(async excel => {
        // excel表示导入的模块对象
        console.log(excel)
        // 获取表格信息
        const res = await getEmpList()
        const res1 = this.transformData(res.data.rows)
        const { data, header } = res1
        console.log(data)
        console.log(res1)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 定义格式转换函数
    transformData(rows) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const header = Object.keys(rows[0])
      const headerZh = header.map(item => map[item])
      const data = rows.map(item => {
        return header.map(key => {
          return item[key]
        })
      })
      return { data, header: headerZh }
    },
    // 点击分配角色
    doEmployees(id) {
      this.curId = id
      this.showDialogEmp = true
    },
    // 子组件的回调
    hSuccessRoels() {
      this.showDialogEmp = false
    }
  }
}
</script>
